var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    fenleiList: [],
    fenleiid: '',
    subfenleiList: {},
    fenleitupian:'',
    fenleiname:''
  },
  onLoad: function(options) {
    // 页面显示
    var that = this;
    that.getfenlei();
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getfenlei();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  getfenlei: function() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.GoodsFenlei).then(function(res) {
      if (res.result == 1) {
        that.setData({
          fenleiList: res.data.fenleiList,
          fenleiid: res.data.fenleiid,
          fenleitupian: res.data.fenleitupian,
          fenleiname: res.data.fenleiname,
          subfenleiList: res.data.subfenleiList
        });
      }
      wx.hideLoading();
    });
  },
  getCurrentfenlei: function(id) {
    let that = this;
    util.request(api.GoodsFenleiCurrent, {
      fenleiid: id
    }, 'GET').then(function(res) {
        that.setData({
          fenleiid: id,
          subfenleiList: res.data.subfenleiList
        });
      });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  switchCate: function(event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (that.data.fenleiid == event.currentTarget.dataset.id) {
      return false;
    }
    let {
      fenleiList
    } = that.data;
    let index = fenleiList.findIndex(v => v.id === event.currentTarget.dataset.id);
    that.setData({
      fenleiname: fenleiList[index].fenleiname,
      fenleitupian: fenleiList[index].fenleitupian
    });
    this.getCurrentfenlei(event.currentTarget.dataset.id);
  }
})